/**
 * 
 */
package dt.inlee.system.action;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import dt.inlee.common.exception.FrameworkException;
import dt.inlee.system.service.SchedulerService;
import dt.inlee.system.vo.JobDetailsBean;
import dt.inlee.system.vo.TriggerBean;

/**
 * 功能说明：
 * @作者： cnoot
 * 创建日期： 2010-5-24
 */
@SuppressWarnings("serial")
@Scope("prototype")
@Results( { @Result(name = "success", location = "/page/test.jsp"),
		@Result(name = "input", location = "/login.jsp") })
@Controller("schedulerAction")
public class SchedulerAction extends ActionSupport implements ModelDriven<JobDetailsBean> {
	@Autowired
	private SchedulerService schedulerService;
	private List<JobDetailsBean> jobDetailBeans = new ArrayList<JobDetailsBean>();
	private JobDetailsBean jobDetailsBean = new JobDetailsBean();
	private String jobId;
	private TriggerBean triggerBean = new TriggerBean();
	private List<TriggerBean> triggerBeans = new ArrayList<TriggerBean>();
	private static final Logger logger = LoggerFactory.getLogger(SchedulerService.class);
	private String executeResult = "false";
	private String triggers;

	/**
	 * 查询所有定时任务
	 * @return
	 * @throws FrameworkException
	 * @throws SchedulerException 
	 */
	@Action(value = "findAllJobs", results = { @Result(type = "json", params = { "excludeNullProperties", "true",
			"excludeProperties", "triggerBean,jobId" }) })
	@SkipValidation()
	public String findAllJobs() throws FrameworkException, SchedulerException {
		logger.debug("findAllJobs()..........");
		jobDetailBeans = this.schedulerService.findJobDetails();
		return SUCCESS;
	}

	/**
	 * 给指定的定时任务添加触发器
	 * @return
	 * @throws FrameworkException
	 * @throws SchedulerException
	 * @throws ClassNotFoundException
	 * @throws NoSuchMethodException
	 * @throws ParseException 
	 */
	@Action(value = "addTrigger", results = { @Result(type = "json", params = { "excludeNullProperties", "true",
			"includeProperties", "executeResult" }) })
	@SkipValidation()
	public String addTriggerToJobDetail() throws FrameworkException, SchedulerException, ClassNotFoundException,
			NoSuchMethodException, ParseException {
		if (this.triggerBean.getOperator().equals("new")) {
			logger.debug("addTriggerToJobDetail()..........");
			if (this.schedulerService.judgeTriggerName(triggerBean.getTriggerName())) {
				this.schedulerService.addTriggerToJobDetail(this.jobDetailsBean.getId(), triggerBean);
				executeResult = "true";
			} else {
				executeResult = "sameNameError";
			}
		} else if (this.triggerBean.getOperator().equals("update")) {
			this.schedulerService.removeTriggers(this.triggerBean.getOldTriggerName().split(","));
			if (this.schedulerService.judgeTriggerName(triggerBean.getTriggerName())) {
				this.schedulerService.addTriggerToJobDetail(this.jobDetailsBean.getId(), triggerBean);
				executeResult = "true";
			} else {
				executeResult = "sameNameError";
			}

		}
		return SUCCESS;
	}

	/**
	 * 根据定时任务名称查询其关联的所有触发器
	 * @return
	 * @throws FrameworkException
	 * @throws SchedulerException
	 * @throws ClassNotFoundException
	 * @throws NoSuchMethodException
	 */
	@Action(value = "findTriggerByJobName", results = { @Result(type = "json", params = { "excludeNullProperties",
			"true", "excludeProperties", "triggerBean,jobId,jobDetails" }) })
	@SkipValidation()
	public String findTriggerByJobName() throws FrameworkException, SchedulerException, ClassNotFoundException,
			NoSuchMethodException {
		logger.debug("findTriggerByJobName()..........");
		triggerBeans = this.schedulerService.findTriggerByJobName(this.jobDetailsBean.getId());
		return SUCCESS;
	}

	/**
	 * 停止触发器
	 * @return
	 * @throws SchedulerException 
	 */
	@Action(value = "pauseTriggers", results = { @Result(type = "json", params = { "excludeNullProperties", "true",
			"includeProperties", "executeResult" }) })
	@SkipValidation()
	public String pauseTriggers() throws SchedulerException {
		this.schedulerService.pauseTriggers(this.triggers.split(","));
		executeResult = "true";
		return SUCCESS;
	}

	/**
	 *	重启触发器
	 * @return
	 * @throws SchedulerException 
	 */
	@Action(value = "resumeTrigger", results = { @Result(type = "json", params = { "excludeNullProperties", "true",
			"includeProperties", "executeResult" }) })
	@SkipValidation()
	public String resumeTrigger() throws SchedulerException {
		this.schedulerService.resumeTrigger(this.triggers.split(","));
		executeResult = "true";
		return SUCCESS;
	}

	/**
	 * 删除触发器
	 * @return
	 * @throws SchedulerException 
	 */
	@Action(value = "removeTriggers", results = { @Result(type = "json", params = { "excludeNullProperties", "true",
			"includeProperties", "executeResult" }) })
	@SkipValidation()
	public String removeTriggers() throws SchedulerException {
		this.schedulerService.removeTriggers(this.triggers.split(","));
		executeResult = "true";
		return SUCCESS;
	}

	/**
	 * 新增定时任务(
	 * @return
	 * @throws SchedulerException 
	 * @throws FrameworkException 
	 */
	@Action(value = "addJobDetail", results = { @Result(type = "json", params = { "excludeNullProperties", "true",
			"includeProperties", "executeResult" }) })
	@SkipValidation()
	public String addJobDetail() throws SchedulerException, FrameworkException {
		try {
			if (schedulerService.judgeBeanAndMethod(this.jobDetailsBean)) {
				if (this.schedulerService.judgeJobName(this.jobDetailsBean.getJobName())) {
					this.schedulerService.addJobDetail(this.jobDetailsBean);
					executeResult = "true";
				} else {
					executeResult = "sameNameError";
				}
			} else {
				executeResult = "errBeanOrMethod";
			}
		} catch (SchedulerException e) {
			executeResult = "false";
			throw e;
		} catch (FrameworkException e) {
			executeResult = "false";
			throw e;
		}
		return SUCCESS;
	}

	/**
	 * 删除定时任务(
	 * @return
	 * @throws SchedulerException 
	 * @throws SchedulerException 
	 * @throws FrameworkException 
	 * @throws FrameworkException 
	 */
	@Action(value = "deleteJobDetail", results = { @Result(type = "json", params = { "excludeNullProperties", "true",
			"includeProperties", "executeResult" }) })
	@SkipValidation()
	public String deleteJobDetail() throws SchedulerException, FrameworkException {
		try {
			this.schedulerService.deleteJob(this.jobDetailsBean.getId());
			executeResult = "true";
		} catch (SchedulerException e) {
			executeResult = "false";
			throw e;
		} catch (FrameworkException e) {
			executeResult = "false";
			throw e;
		}
		return SUCCESS;
	}

	/**
	 * 修改定时任务
	 * @return
	 * @throws SchedulerException 
	 * @throws FrameworkException 
	 */
	@Action(value = "updateJobDetail", results = { @Result(type = "json", params = { "excludeNullProperties", "true",
			"includeProperties", "executeResult" }) })
	@SkipValidation()
	public String updateJobDetail() throws SchedulerException, FrameworkException {
		logger.debug("updateJobDetail()........jobDetailsBean.." + jobDetailsBean);
		logger.debug("updateJobDetail()........jobDetailsBean. is null." + (jobDetailsBean == null));
		if (schedulerService.judgeBeanAndMethod(this.jobDetailsBean)) {
			if (this.schedulerService.judgeJobName(this.jobDetailsBean.getId(), this.jobDetailsBean.getJobName())) {
				this.schedulerService.updateJobDetail(jobDetailsBean);
				executeResult = "true";
			} else {
				executeResult = "sameNameError";
			}

		} else {
			executeResult = "errBeanOrMethod";
		}
		return SUCCESS;
	}

	public List<JobDetailsBean> getJobDetailBeans() {
		return jobDetailBeans;
	}

	public String getJobId() {
		return jobId;
	}

	public void setJobId(String jobId) {
		this.jobId = jobId;
	}

	public TriggerBean getTriggerBean() {
		return triggerBean;
	}

	public void setTriggerBean(TriggerBean triggerBean) {
		this.triggerBean = triggerBean;
	}

	public String getExecuteResult() {
		return executeResult;
	}

	public List<TriggerBean> getTriggerBeans() {
		return triggerBeans;
	}

	public void setTriggers(String triggers) {
		this.triggers = triggers;
	}

	public JobDetailsBean getJobDetailsBean() {
		return jobDetailsBean;
	}

	public void setJobDetailsBean(JobDetailsBean jobDetailsBean) {
		this.jobDetailsBean = jobDetailsBean;
	}

	public JobDetailsBean getModel() {
		return jobDetailsBean;
	}
}
